import Base from '../model/base/Base.js'

export class fuck extends Base {
  constructor () {
    super({
      name: 'ad[骂]',
      dsc: '解禁',
      event: 'message',
      priority: -1,
      rule: [
        {
          reg: /^#?骂(他|她|它|ta)?((\d+)(次))?$/i,
          fnc: 'fuck'
        },
        {
          reg: /^#?ad设置骂人(开启|关闭)$/i,
          fnc: 'fuckkg'
        }
      ]
    })
  }

  async fuckkg (e) {
    let aw = e.msg.includes('开启')
    if (!e.isMaster) return this.reply('你没有权限')
    // this.cm.tj().catch()
    if (aw) redis.set('ad:maren', 'true')
    else redis.set('ad:maren', 'false')
  }

  async fuck (e) {
    let kg = await redis.get('ad:maren')
    kg = kg == 'true'
    if (!kg) {
      this.reply('骂人未开启，请发送#ad设置骂人开启')
      return true
    }
    // this.cm.tj().catch()
    if (!(await this.cm.checkBot(e))) return false
    let css = e.msg.includes('次')
    let cs
    if (css) {
      cs = e.msg.match(/(\d+)/)
      cs = cs[0]
    }
    let targetid = e.at ? e.at : '123456'
    targetid = e.atall ? 'all' : targetid
    if (await this.cm.check(e.at ? e.at : '1')) {
      await e.reply('你tm还想骂他是吧')
      return false
    }
    if (e.at === e.self_id) {
      await e.reply('你tm还想骂我是吧')
      return false
    }
    if (!css) {
      let res = await fetch('https://api.yunxiyuan.cyou/fuck/?type=text')
      res = await res.text()
      await this.e.reply([segment.at(targetid), res])
    } else {
      let response = await fetch(
        `https://api.yunxiyuan.cyou/fuck/?type=json&num=${cs}`
      )
      let data = await response.json()
      for (let phrase of data.text) {
        let trimmedPhrase = phrase.trim()
        await this.e.reply([segment.at(targetid), trimmedPhrase])
      }
    }
  }
}
